package com.ans.dragoncloud.controller;

import com.ans.dragoncloud.pojo.Student;
import com.ans.dragoncloud.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@RestController
@Controller
public class StudentController {

    @Autowired
    private StudentService service;

    //http://localhost:8080/student/findById/1
    @GetMapping("/student/findById/{id}")
    @ResponseBody
    public Map<String,Object> findById(@PathVariable("id") Integer id) {
        Map<String,Object> map = new HashMap<>();
        Student stu = service.getStudentById(id);
        map.put("student",stu);
        return map;
    }

    //http://localhost:8080/student/findAll
    @GetMapping("/student/findAll")
    public String findAll(Model model) {
        List<Student> userList = service.getAllStudents();
        model.addAttribute("userList",userList);
        return "showStudent";
    }

}
